<?php
    /**
     * Returns true if user comes from trusted IP
     * 
     * @return boolean whether user comes from trusted IP
     */
    function is_trusted_ip()
    {
        // Trusted IPs
        $arIps = array(
            ''
            // InterAteitis office
            , '81.7.79.76'
        );
        
        $totalR = count($arIps);
        $clientIp = getenv('REMOTE_ADDR');
        $clientIpLen = strlen($clientIp);
        $isFound = false;
        $pos = 0;
        
        while( $pos < $totalR )
        {
            //echo "IP to check: " . $arIps[$pos] . "<br />";
            if( strncmp($clientIp, $arIps[$pos], $clientIpLen) == 0 )
            {
                return true;
            }
            $pos++;
        }
        
        return false;
    }
function compareArr($a, $b)
{
    if ($a['obj']->getPriority() == $b['obj']->getPriority()) {
        return 0;
    }
    return ($a['obj']->getPriority() < $b['obj']->getPriority()) ? 1 : -1;
}
/**
 * Returns true if user is logged in and has any of role from array
 * 
 * @param array $arRoleIds - array(MyConstants::ROLE_ADMIN, ...)
 * @return boolean whether user has role
 */
function has_any_role_id($arRoleIds)
{
    $has = false;
    
    if( ($userId = get_user_id()) )
    {
        $user = UserPeer::retrieveByPK($userId);
        for( $i = 0; ($has == false) && ($i < count($arRoleIds)); $i++ )
        {
        	//debugvar('User roles [' . $user->getRole() . '] contains role ID #' . $arRoleIds[$i]);
            if( preg_match("/\b" . (int)$arRoleIds[$i] . "\b/", $user->getRoles()) )
            {
                $has = true;
            }
        }
    }
    
    return $has;
}
if (!function_exists("debugvar"))
{
    /**
     * 
     * returns svn nrevision number from revision.txt file, wich is ubdated by project commiting
     */
    function getSvnRevizionNumber()
    {
        $svn = File('revision.txt');
        $svnrev = $svn[0];
        unset($svn);
        return substr($svnrev
                    , 6
                    , (strlen(substr($svnrev,6)))-2);
    }
    function debugvar($var, $title = '')
    {
        if( (defined('SF_DEBUG') && SF_DEBUG) && (defined('SF_ENVIRONMENT') && (SF_ENVIRONMENT != 'prod')) )
        {
            ob_start();
            if( $title )
                echo "$title\n";
            print_r($var);
            $out = ob_get_clean();
            echo "<pre>";
            echo htmlentities($out);
            echo "</pre>";
        }
    }
    
    /**
     * Returns true if user has such role ID
     *
     * @param integer $roleId role ID from MyConstants::ROLE_NNN
     * @return boolean
     */
    function has_role_id($roleId)
    {
        $has = false;
        
        if( ($userId = get_user_id()) )
        {
            $user = User::getOrCreate($userId);
            if( preg_match("/\b" . (int)$roleId . "\b/", $user->getRoles()) )
            {
                $has = true;
            }
        }
        
        return $has;
    }
    

    function get_culture()
    {
        return sfContext::getInstance()->getUser()->getCulture();
    }

    function log_this($str, $logLevel = SF_LOG_INFO)
    {
        if( $str )
            sfContext::getInstance()->getLogger()->log($str, SF_LOG_INFO);
    }

   /**
     * Returns ID of logged in user or 0 if session timeout
     *
     * @return integer
     */
    function get_user_id() 
    {
        $user = sfContext::getInstance()->getUser();
        if( $user->isAuthenticated() )
            return (int)$user->getAttribute('userId');

        // Session timeout
        return 0;
    }
    /**
     * 
     * returns only domain name
     */
    function get_domain_name()
    {
        $serverName = $_SERVER['SERVER_NAME'];
        // get last two segments of host name
        preg_match('/[^.]+\.[^.]+$/', $serverName, $matches);
        return $matches[0];
    }
    
}


?>
